<?php
/**
 * 标签
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */

namespace App\Http\Controllers\Api\V1\Admin;


use App\Models\Article;
use App\Models\Comment;
use App\Models\Live;
use App\Models\Movie;
use App\Models\MovieCategory;
use App\Models\User;
use App\Models\UserGiftLog;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class StatController extends BaseController
{
    public function index()
    {
        $type = request()->input('type');
        $data=[];
        if($type==12){
            $data=$this->userinfo();
        }elseif ($type==11){
            $data=$this->videoinfo();
        }elseif ($type==10){
            $data=$this->Commentinfo();
        }elseif ($type==9){
            $data=$this->articleinfo();
        }elseif ($type==8){
            $data=$this->liveinfo();
        }elseif ($type==7){
            $data=$this->giftinfo();
        }
        return $this->success('成功', $data);
    }
    //用户统计
    public function userinfo()
    {
        $sex = [0, 0, 0];
        $lists = User::all();
        $s = date('d');
        $e = date('Y');
        $date_array = array();//月注册用户数据集
        $yue_array = array();//月下标数据集
        $year_date_array=array();
        $year_array=array();
        $i = 0;
        while ($i < $s) {
            $date_array['reg'][Carbon::today()->subDays($s - $i - 1)->format('Y-m-d')] = 0;
            $date_array['not'][Carbon::today()->subDays($s - $i - 1)->format('Y-m-d')] = 0;
            $date_array['man'][Carbon::today()->subDays($s - $i - 1)->format('Y-m-d')] = 0;
            $date_array['woman'][Carbon::today()->subDays($s - $i - 1)->format('Y-m-d')] = 0;
            array_push($yue_array, Carbon::today()->subDays($s - $i - 1)->format('Y-m-d'));
            $i++;
        }
        $ii = $e-1;
        while ($ii <= $e) {
            $year_date_array['reg'][Carbon::today()->subYears( $e-$ii )->format('Y')] = 0;
            $year_date_array['not'][Carbon::today()->subYears($e-$ii )->format('Y')] = 0;
            $year_date_array['man'][Carbon::today()->subYears( $e-$ii )->format('Y')] = 0;
            $year_date_array['woman'][Carbon::today()->subYears( $e-$ii )->format('Y')] = 0;
            array_push($year_array, Carbon::today()->subYears($e-$ii )->format('Y'));
            $ii++;
        }
        foreach ($lists as $list) {
            if ($list->sex == 0) {
                $sex[0] = $sex[0] + 1;
            } elseif ($list->sex == 1) {
                $sex[1] = $sex[1] + 1;
            } elseif ($list->sex == 2) {
                $sex[2] = $sex[2] + 1;
            }
            foreach ($date_array['reg'] as $k => $date) {
                if ($k == $list->created_at->toDateString()) {
                    $date_array['reg'][$k] += 1;
                    if ($list->sex == 0) {
                        $date_array['not'][$k] += 1;
                    } elseif ($list->sex == 1) {
                        $date_array['man'][$k] += 1;
                    } elseif ($list->sex == 2) {
                        $date_array['woman'][$k] += 1;
                    }
                }
            }
            foreach ($year_date_array['reg'] as $key => $value) {
                if ($key == date('Y',strtotime((string)($list->created_at)))) {
                    $year_date_array['reg'][$key] += 1;
                    if ($list->sex == 0) {
                        $year_date_array['not'][$key] += 1;
                    } elseif ($list->sex == 1) {
                        $year_date_array['man'][$key] += 1;
                    } elseif ($list->sex == 2) {
                        $year_date_array['woman'][$key] += 1;
                    }
                }
            }
        }
        $data = ['sex' => $sex,
            'yue' =>
                ['yuedate' => $yue_array,
                    'reg' => Arr::flatten($date_array['reg']),
                    'man' => Arr::flatten($date_array['man']),
                    'woman' => Arr::flatten($date_array['woman']),
                    'not' => Arr::flatten($date_array['not']),
                ],
            'year'=>
                [
                    'yuedate' => $year_array,
                    'reg' => Arr::flatten($year_date_array['reg']),
                    'man' => Arr::flatten($year_date_array['man']),
                    'woman' => Arr::flatten($year_date_array['woman']),
                    'not' => Arr::flatten($year_date_array['not']),
                ]
        ];
        return $data;
    }
    //短视频统计
    public function videoinfo()
    {
        $movie_menu=MovieCategory::pluck('name');
        $movie_menu_data=MovieCategory::pluck('id');
        foreach ($movie_menu_data as $movie_type_id){
            $move_type_count[$movie_type_id]=Movie::where('category_id',$movie_type_id)->count();
        }
        $movie_count=array_sum($move_type_count);
        $s = date('d');
        $e = date('Y');
        $date_array = array();//月注册用户数据集
        $yue_array = array();//月下标数据集
        $year_date_array=array();
        $year_array=array();
        $i = 0;
        while ($i < $s) {
            $date_array['reg'][Carbon::today()->subDays($s - $i - 1)->format('Y-m-d')] = 0;
            array_push($yue_array, Carbon::today()->subDays($s - $i - 1)->format('Y-m-d'));
            $i++;
        }
        $ii = $e-1;
        while ($ii <= $e) {
            $year_date_array['reg'][Carbon::today()->subYears( $e-$ii )->format('Y')] = 0;
            array_push($year_array, Carbon::today()->subYears($e-$ii )->format('Y'));
            $ii++;
        }
        $lists=Movie::all();
        foreach ($lists as $list) {
            foreach ($date_array['reg'] as $k => $date) {
                if ($k == $list->created_at->toDateString()) {
                    $date_array['reg'][$k] += 1;
                }
            }
            foreach ($year_date_array['reg'] as $key => $value) {
                if ($key == date('Y',strtotime((string)($list->created_at)))) {
                    $year_date_array['reg'][$key] += 1;
                }
            }
        }
        $data = [
            'count' => $movie_count,
            'pie'=>[
                'menu'=>$movie_menu->toArray(),
                'data'=>Arr::flatten($move_type_count),
            ],
            'yue' =>
                [
                    'yuedate' => $yue_array,
                    'reg' => Arr::flatten($date_array['reg']),
                ],
            'year'=>
                [
                    'yuedate' => $year_array,
                    'reg' => Arr::flatten($year_date_array['reg']),
                ]
        ];
        return $data;
    }
    //评论统计
    public function Commentinfo()
    {
        $comment=new Comment();
        $arinfo[0]=$comment::where('type',1)->count();//视频总计
        $arinfo[1]=$comment::where('type',2)->count();//图文总计
        $ar_video_menu=$comment->where('comment.type',1)
            ->groupBy('vid')
            ->leftJoin('video', 'comment.vid', '=', 'video.id')
            ->select('comment.id','comment.vid','comment.like_num',DB::raw('count(vid) as vids'),'video.title')
            ->limit(5)
            ->orderBy('vids')
            ->having('vids','>',10)
            ->get()
            ->toArray();//视频最热10条目录
        $temp=[];
        foreach ($ar_video_menu as $k=>$v){
            $temp[$v['title']]=$v['vids'];
        }
        [$video_menu, $video_data] = Arr::divide($temp);

        $ar_text_menu=$comment->where('comment.type',2)
            ->groupBy('vid')
            ->leftJoin('article', 'comment.vid', '=', 'article.id')
            ->select('comment.id','comment.vid','comment.like_num',DB::raw('count(vid) as vids'),'article.title')
            ->limit(5)
            ->orderBy('vids')
            ->having('vids','>',10)
            ->get()
            ->toArray();//视频最热10条目录
        $sss=[];
        foreach ($ar_text_menu as $k=>$v){
            $sss[$v['title']]=$v['vids'];
        }
        [$text_menu, $text_data] = Arr::divide($sss);
        $data = ['count' => $arinfo,
            'video' =>
                ['videomenu' => $video_menu, 'videodata' => $video_data,
                ],
            'textimg'=>
                [
                    'textmenu' => $text_menu,
                    'textdata' => $text_data,
                ]
        ];
        return $data;
    }
    //图文统计
    public function articleinfo()
    {
        $comment=new Article();
        $arinfo[0]=$comment::where('type',2)->count();//视频总计
        $arinfo[1]=$comment::where('type',1)->count();//图文总计
        $ar_video_menu=$comment->where('type',2)
            ->groupBy('comment_num')
            ->select('id',DB::raw('max(comment_num) as comment_nums'),'title')
            ->limit(5)
            ->orderBy('comment_nums')
            ->having('comment_nums','>',1)
            ->get()
            ->toArray();//视频最热10条目录
        $ss=[];
        foreach ($ar_video_menu as $k=>$v){
            $ss[$v['title']]=$v['comment_nums'];
        }
        [$video_menu, $video_data] = Arr::divide($ss);

        $ar_text_menu=$comment->where('type',1)
            ->groupBy('comment_num')
            ->select('id',DB::raw('max(comment_num) as comment_nums'),'title')
            ->limit(5)
            ->orderBy('comment_nums')
            ->having('comment_nums','>',1)
            ->get()
            ->toArray();//视频最热10条目录
        $sss=[];
        foreach ($ar_text_menu as $k=>$v){
            $sss[$v['title']]=$v['comment_nums'];
        }
        [$text_menu, $text_data] = Arr::divide($sss);
        $data = ['count' => $arinfo,
            'video' =>
                ['videomenu' => $video_menu, 'videodata' => $video_data,
                ],
            'textimg'=>
                [
                    'textmenu' => $text_menu,
                    'textdata' => $text_data,
                ]
        ];
        return $data;
    }
    //直播统计
    public function liveinfo()
    {
        $comment=new Live();
        $arinfo[0]=$comment::where('status',0)->count();//下播
        $arinfo[1]=$comment::where('status',1)->count();//上播
        $ar_video_menu=$comment
            ->groupBy('like_num')
            ->select('id',DB::raw('max(like_num) as like_nums'),'title')
            ->limit(5)
            ->orderBy('like_nums')
            ->having('like_nums','>',1)
            ->get()
            ->toArray();//视频最热10条目录
        $ss=[];
        foreach ($ar_video_menu as $k=>$v){
            $ss[$v['title']]=$v['like_nums'];
        }
        [$video_menu, $video_data] = Arr::divide($ss);

        $ar_text_menu=$comment
            ->groupBy('view_num')
            ->select('id',DB::raw('max(view_num) as view_nums'),'title')
            ->limit(5)
            ->orderBy('view_nums')
            ->having('view_nums','>',1)
            ->get()
            ->toArray();//视频最热10条目录
        $sss=[];
        foreach ($ar_text_menu as $k=>$v){
            $sss[$v['title']]=$v['view_nums'];
        }
        [$text_menu, $text_data] = Arr::divide($sss);
        $data = ['count' => $arinfo,
            'video' =>
                ['videomenu' => $video_menu, 'videodata' => $video_data,
                ],
            'textimg'=>
                [
                    'textmenu' => $text_menu,
                    'textdata' => $text_data,
                ]
        ];
        return $data;
    }
    //礼物统计
    public function giftinfo()
    {
        $comment=new UserGiftLog();
        $arinfo=$comment::count();//礼物
        $ar_video_menu=$comment
            ->groupBy('gift_id')
            ->leftJoin('gift', 'user_gift_log.gift_id', '=', 'gift.id')
            ->select('gift.id',DB::raw('max(gift_id) as gift_ids'),'gift.name')
            ->limit(5)
            ->orderBy('gift_ids')
            ->having('gift_ids','>',1)
            ->get()
            ->toArray();//视频最热10条目录
        $ss=[];
        foreach ($ar_video_menu as $k=>$v){
            $ss[$v['name']]=$v['gift_ids'];
        }
        [$video_menu, $video_data] = Arr::divide($ss);
        $data = ['count' => $arinfo,
            'video' =>
                ['videomenu' => $video_menu, 'videodata' => $video_data,
                ],
        ];
        return $data;
    }
}
